package com.example.http.provider.service.impl;

import com.example.http.provider.bean.User;
import com.example.http.provider.service.UserService;

import java.util.HashMap;

public class UserServiceImpl implements UserService {

    private static final HashMap<Integer, User> USER_MAP = new HashMap<>();

    static {
        USER_MAP.put(1, new User(1, "tom", 11));
        USER_MAP.put(2, new User(2, "lucy", 12));
        USER_MAP.put(3, new User(3, "jack", 13));
        USER_MAP.put(4, new User(4, "mary", 14));
    }

    @Override
    public User add(User user) {
        Integer max = USER_MAP.keySet().stream().max(Integer::compareTo).orElse(0);
        Integer id = max + 1;
        user.setId(id);
        USER_MAP.put(id, user);
        return user;
    }

    @Override
    public User get(Integer id) {
        return USER_MAP.get(id);
    }
}
